Walkthrough 11-2: Transform basic JSON, Java, and XML data structures

In this walkthrough, you continue to work with the JSON flight data posted to the flow. You will:

·       Write scripts to transform the JSON payload to various JSON and Java structures.

·       Write scripts to transform the JSON payload to various XML structures.

Starting file

If you did not complete the previous walkthrough, you can get a starting file here. This file is also located in the solutions folder of the student files ZIP located in the Course Resources.

Write expressions to transform JSON to various Java structures

1.     Return to the Transform Message properties view for the transformation in postFlight.

2.     In the output drop-down menu, make sure the payload output expression is selected.

3.     Change the output type from java to json.

4.     Type a period after payload and double-click price in the auto-completion menu.

 

5.     Look at the preview.

 

6.     Change the output type from json to java.

 

7.     Change the DataWeave expression to data: payload.price.

 

8.     Change the output type from java to json.

 

9.     Change the DataWeave expression to data: payload.

 

10.  Change the output type from json to java.

 

11.  Change the DataWeave expression to data: {}.

12.  Add a field called hub and set it to “MUA”.

 

13.  Change the output type from java to json.

 

14.  Add a field called code and use auto-completion to set it to the toAirportCode property of the payload.

15.  Add a field called airline and set it to the airline property of the payload.

 

Write an expression to output data as XML

16.  In the output drop-down menu, select Variable – DW output.

17.  Change the DataWeave expression to data: payload.

18.  Look at the preview; you should now see XML.

 

19.  In the output drop-down menu, select Payload.

20.  Copy the DataWeave expression.

21.  Switch back to Variable – DWoutput and replace the DataWeave expression with the one you just copied.

 

22.  Modify the expression so the code and airline properties are child elements of a new element called flight.

 

23.  Modify the expression so the airline is an attribute of the flight element.

 

Debug the application

24.  In the Logger properties view, set the message to the value of the DWoutput variable.

 

25.  Save the file to redeploy the project in debug mode.

26.  In Advanced REST Client, post the same request to http://localhost:8081/api/flights.

27.  In the Mule Debugger, step to the Logger; you should see the transformed JSON payload and that the DWoutput variable has a mime-type of application/xml.

 

28.  Step through the application and switch perspectives.

29.  Look at the Logger output in the console; you should see the XML.

 



Did you complete the walkthrough?

  Yes, I completed the walkthrough

  No, I did not complete the walkthrough

  I completed part of the walkthrough


Comments and/or feedback